<template>
	<view class="container">
		<view class="form-box">
			<view class="form">
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">姓名</view>
						 <input class="input" placeholder="请输入姓名" v-model="form.accept_name" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">电话</view>
						 <input class="input" placeholder="请获取电话号" v-model="form.mobile" placeholder-class="text-color-assist"/>
						 <button type="primary" size="mini" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取</button>  
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">地址</view>
						 <input class="input" placeholder="请选择地址" v-model="form.street" placeholder-class="text-color-assist" @click="getLocation"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">门牌号</view>
						 <input class="input" placeholder="请输入详细地址" v-model="form.door_number" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">性别</view>
						<view class="radio-group">
							<view class="radio" :class="{'checked': !form.sex}" style="margin-right: 30rpx;" @tap="form.sex=0">先生</view>
							<view class="radio" :class="{'checked': form.sex}" @tap="form.sex=1">女士</view>
						</view>
					</view>
				</list-cell>
			</view>
			<view class="btn-section">
				<button type="primary" size="default" @tap="save">保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	import listCell from '@/components/list-cell/list-cell'
	
	export default {
		components: {
			listCell
		},
		data() {
			return {
				form: {
					accept_name: '',
					sex: 0,
					mobile: '',
					street: '',
					door_number: '',
					iv:'',
					encryptedData:'',
					longitude:'',
					latitude:'',
					id:''
				}
			}
		},
		async onLoad({id}) {
			//为了方便演示，这里用本地缓存
			if(id) {
				this.id = id
				this.$http.get('/address/one', {params:{id:id}}).then(res => {
					// console.log(res.data.data)
					this.form = res.data.data
				}).catch(err => {
				
				})
				// this.form = this.$store.state.addresses.find(item => item.id == id)
			}
		},
		methods: {
			save() {
				// 添加数据
				this.$http.post('/address/add',{accept_name:this.form.accept_name,sex:this.form.sex,mobile:this.form.mobile,street:this.form.street,door_number:this.form.door_number,longitude:this.form.longitude,latitude:this.form.latitude,id:this.id}).then(res => {
					console.log(res)
					
				}).catch(err => {
				
				})
				uni.navigateBack()
			},
			getPhoneNumber(e) {  
				let mobile = uni.getStorageSync('getPhoneNumber')
				if(mobile){
					this.form.mobile = mobile
				}
				if(e.detail.errMsg == 'getPhoneNumber:ok'){
					let code = e.detail.code
					let iv = e.detail.iv
					let encryptedData = e.detail.encryptedData
					this.$http.post('/wx/getPhoneNumber', {code:code,iv:iv,encryptedData:encryptedData}).then(res => {
						this.form.mobile = res.data.data
						uni.setStorageSync('getPhoneNumber',this.form.mobile)
					}).catch(err => {
					
					})
				}else{
					this.form.mobile = ''
				}
			},
			getLocation(){
				console.log('选择地址')
				uni.chooseLocation({
					success: (e) => {
						// console.log('getLocation',e)
						this.form.street = e.name
						this.form.door_number = e.address
						this.form.longitude = e.longitude
						this.form.latitude = e.latitude
					},
					fail:(e)=>{
						uni.showToast({
							title:'右上角三个点设置位置授权',
							icon:'none',
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	.container{
		background-color: #fff;
	}
	
	.form-box {
		width: 100%;
		height: 100%;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		color: $text-color-base;
		
		.form-input {
			display: flex;
			align-items: center;
			width: 100%;
		}
		
		.label {
			width: 200rpx;
			font-size: $font-size-lg;
			color: $text-color-base;
			font-weight: 500;
		}
		
		.input {
			flex: 1;
			display: flex;
			align-items: center;
		}
		
		.radio-group {
			display: flex;
			justify-content: flex-start;
			
			.radio {
				padding: 10rpx 30rpx;
				border-radius: 6rpx;
				border: 2rpx solid $text-color-assist;
				color: $text-color-assist;
				font-size: $font-size-base;
				
				&.checked {
					background-color: $color-primary;
					color: $text-color-white;
					border: 2rpx solid $color-primary;
				}
			}
		}
		
		.btn-section {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			
			button {
				font-size: $font-size-base;
				height: 90rpx;
				border-radius: 50rem !important;
				width: 85%;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
